import 'package:flutter/material.dart';

void _goHome(BuildContext ctx) {
    Navigator.popUntil(ctx, ModalRoute.withName(Navigator.defaultRouteName));
}

class Page1 extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
                title: Text("Page 1"),
                backgroundColor: Theme
                    .of(context)
                    .accentColor,
            ),
            body: Center(
                child: FlatButton(
                    onPressed: () {
                        Navigator.push(context, PageTwo());
                    },
                    child: Row(
                        mainAxisSize: MainAxisSize.min,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[Icon(Icons.favorite), Text("Navigator.push page 2")],
                    )),
            ),
        );
    }
}

class PageTwo extends MaterialPageRoute<Null> {
    PageTwo() : super(builder: _build);
    
    static Widget _build(BuildContext ctx) {
        return Scaffold(
            appBar: AppBar(
                title: Text("page 2"),
            ),
            body: Center(
                child: IconButton(
                    icon: Icon(Icons.print),
                    tooltip: "Navigator.pushNamed page 3",
                    onPressed: () {
                        Navigator.pushNamed(ctx, "/thirdPage");//这里的的name需要在main.dart的routes里注册
                    })));
    }
}

class Page3Body extends StatefulWidget {
    @override
    State<StatefulWidget> createState() {
        return Page3State();
    }
}

class Page3State extends State<Page3Body> {
    String res = "waiting jump";
    
    _gotoPage5(BuildContext ctx) async {
        final result = await Navigator.push(ctx, Page5());
        
        Scaffold.of(ctx)
            ..removeCurrentSnackBar()
            ..showSnackBar(SnackBar(content: Text("$result")));
        
        setState(() {
            res = result;
        });
    }
    
    @override
    Widget build(BuildContext ctx) {
        //return Builder(//if this code in Scaffold body then need add a Builder...comment by cjz
        //  builder: (BuildContext context) { // Create an inner BuildContext so that the onPressed methods can refer to the Scaffold with Scaffold.of().
        return Center(
            child: Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                    RaisedButton(
                        child: Column(
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[Text("go home"), Icon(Icons.feedback)],
                        ),
                        onPressed: () {
                            _goHome(ctx);
                            //Navigator.popUntil(ctx, ModalRoute.withName(Navigator.defaultRouteName));
                        },
                    ),
                    Divider(),
                    FlatButton(
                        child: Row(
                            mainAxisSize: MainAxisSize.min,
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                                Icon(Icons.add_location),
                                Text("click here go to page 4 with parameter"),
                            ],
                        ),
                        onPressed: () {
                            Navigator.push(ctx, Page4(titleName: "pass from page 3"));
                        },
                    ),
                    Divider(),
                    RaisedButton(
                        child: Row(
                            mainAxisSize: MainAxisSize.min,
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[Icon(Icons.add_location), Text("click here go to page 5\n give back result:$res")],
                        ),
                        onPressed: () {
                            _gotoPage5(ctx); // Create an inner BuildContext so that the onPressed methods can refer to the Scaffold with Scaffold.of().
                        },
                    ),
                ],
            ),
        );
        // }
        
        //);
    }
}

class PageThree extends MaterialPageRoute<Null> {
    PageThree()
        : super(builder: (BuildContext ctx) {
        return Scaffold(
            appBar: AppBar(
                title: Text("Page 3"),
                automaticallyImplyLeading: false,//appbar去掉返回键
                actions: <Widget>[
                    IconButton(
                        icon: Icon(Icons.close),
                        onPressed: () {
                            Navigator.pop(ctx);
                        })
                ],
            ),
            body: Page3Body());
    });
}

class Page4 extends MaterialPageRoute<Null> {
    final String titleName;
    
    Page4({@required this.titleName})
        : super(builder: (BuildContext ctx) {
        return Scaffold(
            appBar: AppBar(
                title: Text("p 4"),
                backgroundColor: Theme
                    .of(ctx)
                    .accentColor,
            ),
            body: Center(
                child: Text("got \"$titleName\" parameter!"),
            ),
        );
    });
}

class Page5 extends MaterialPageRoute<String> {
    Page5()
        : super(builder: (BuildContext ctx) {
        return WillPopScope(
            // catch back key event to pass params
            onWillPop: () {
                Navigator.pop(ctx, "no! by key event");
            },
            child: Scaffold(
                appBar: AppBar(
                    title: Text("p 5"),
                    leading: IconButton(
                        tooltip: 'Custom back',
                        icon: Icon(Icons.arrow_back),
                        onPressed: () {
                            Navigator.pop(ctx, "no! by appbar");
                        },
                    ),
                ),
                body: Center(
                    child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        mainAxisSize: MainAxisSize.max,
                        children: <Widget>[
                            Container(
                                margin: EdgeInsets.all(20.0),
                                child: RaisedButton(
                                    child: Text("ok"),
                                    onPressed: () {
                                        Navigator.pop(ctx, "ok! by button");
                                    },
                                ),
                            ),
                            Container(
                                margin: EdgeInsets.all(20.0),
                                child: RaisedButton(
                                    child: Text("no"),
                                    onPressed: () {
                                        Navigator.pop(ctx, "no! by button");
                                    },
                                ),
                            )
                        ],
                    ),
                )));
    });
}
